<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="日志类型" prop="logType">
            <el-select v-model="queryParams.logType" placeholder="日志类型" clearable style="width: 200px">
               <el-option label="系统日志" value="system" />
               <el-option label="操作日志" value="operation" />
               <el-option label="错误日志" value="error" />
            </el-select>
         </el-form-item>
         <el-form-item label="设备编号" prop="deviceCode">
            <el-input
               v-model="queryParams.deviceCode"
               placeholder="请输入设备编号"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="日期范围" prop="dateRange">
            <el-date-picker
               v-model="dateRange"
               type="datetimerange"
               range-separator="至"
               start-placeholder="开始日期"
               end-placeholder="结束日期"
               style="width: 240px"
            />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               type="danger"
               plain
               icon="Delete"
               @click="handleClean"
               v-hasPermi="['data:device-log:remove']"
            >清空日志</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
               v-hasPermi="['data:device-log:export']"
            >导出</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="logList">
         <el-table-column type="index" label="序号" width="60" align="center" />
         <el-table-column label="日志类型" align="center" prop="logType" :show-overflow-tooltip="true">
            <template #default="scope">
               <el-tag v-if="scope.row.logType === 'system'">系统日志</el-tag>
               <el-tag v-else-if="scope.row.logType === 'operation'" type="success">操作日志</el-tag>
               <el-tag v-else-if="scope.row.logType === 'error'" type="danger">错误日志</el-tag>
               <span v-else>{{ scope.row.logType }}</span>
            </template>
         </el-table-column>
         <el-table-column label="设备编号" align="center" prop="deviceCode" :show-overflow-tooltip="true" />
         <el-table-column label="日志内容" align="center" prop="logContent" :show-overflow-tooltip="true" />
         <el-table-column label="日志级别" align="center" prop="logLevel">
            <template #default="scope">
               <el-tag v-if="scope.row.logLevel === 'info'" type="info">INFO</el-tag>
               <el-tag v-else-if="scope.row.logLevel === 'warn'" type="warning">WARN</el-tag>
               <el-tag v-else-if="scope.row.logLevel === 'error'" type="danger">ERROR</el-tag>
               <span v-else>{{ scope.row.logLevel }}</span>
            </template>
         </el-table-column>
         <el-table-column label="记录时间" align="center" prop="logTime" width="180">
            <template #default="scope">
               <span>{{ parseTime(scope.row.logTime) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['data:device-log:query']">详情</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />

      <!-- 日志详情对话框 -->
      <el-dialog title="日志详情" v-model="open" width="700px" append-to-body>
         <el-descriptions :column="2" border>
            <el-descriptions-item label="日志类型">{{ form.logType }}</el-descriptions-item>
            <el-descriptions-item label="设备编号">{{ form.deviceCode }}</el-descriptions-item>
            <el-descriptions-item label="日志级别">
               <el-tag v-if="form.logLevel === 'info'" type="info">INFO</el-tag>
               <el-tag v-else-if="form.logLevel === 'warn'" type="warning">WARN</el-tag>
               <el-tag v-else-if="form.logLevel === 'error'" type="danger">ERROR</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="记录时间">{{ parseTime(form.logTime) }}</el-descriptions-item>
            <el-descriptions-item label="日志内容" :span="2">
               <pre style="white-space: pre-wrap; word-wrap: break-word;">{{ form.logContent }}</pre>
            </el-descriptions-item>
         </el-descriptions>
         <template #footer>
            <div class="dialog-footer">
               <el-button @click="open = false">关 闭</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="DeviceLog">
import { listDeviceLog, getDeviceLog, cleanDeviceLog } from "@/api/data/device-log";

const { proxy } = getCurrentInstance();

const logList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref([]);
const form = ref({});

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  logType: undefined,
  deviceCode: undefined
});

/** 查询设备日志列表 */
function getList() {
  loading.value = true;
  // TODO: 调用实际API
  // listDeviceLog(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
  //   logList.value = response.rows;
  //   total.value = response.total;
  //   loading.value = false;
  // });
  
  setTimeout(() => {
    logList.value = [];
    total.value = 0;
    loading.value = false;
    proxy.$modal.msgWarning("设备日志功能开发中，请等待后端接口...");
  }, 500);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 查看详情 */
function handleView(row) {
  // TODO: 获取详情
  // getDeviceLog(row.logId).then(response => {
  //   form.value = response.data;
  //   open.value = true;
  // });
  form.value = row;
  open.value = true;
}

/** 清空日志 */
function handleClean() {
  proxy.$modal.confirm('是否确认清空所有设备日志数据？').then(function() {
    // return cleanDeviceLog();
    proxy.$modal.msgWarning("清空日志功能开发中...");
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("清空成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("data/device-log/export", {
    ...queryParams.value
  }, `device-log_${new Date().getTime()}.xlsx`);
}

getList();
</script>

